package com.ruoyi.fitsculpt.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fitsculpt.domain.EasyfitExercisePlans;
import com.ruoyi.fitsculpt.service.IEasyfitExercisePlansService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.ruoyi.fitsculpt.domain.EasyfitUsers;
import com.ruoyi.fitsculpt.service.IEasyfitUsersService;

/**
 * 运动计划Controller
 *
 * @author metaee
 * @date 2025-02-27
 */
@Api(tags = "运动计划管理")
@RestController
@RequestMapping("/fitsculpt/exercise/plans")
public class EasyfitExercisePlansController extends BaseController {
    @Autowired
    private IEasyfitExercisePlansService easyfitExercisePlansService;

    @Autowired
    private IEasyfitUsersService easyfitUsersService;

    /**
     * 查询运动计划列表
     */
    @ApiOperation("查询运动计划列表")
    @GetMapping("/list")
    public TableDataInfo list(EasyfitExercisePlans easyfitExercisePlans) {
        startPage();
        List<EasyfitExercisePlans> list = easyfitExercisePlansService.selectEasyfitExercisePlansList(easyfitExercisePlans);
        for (EasyfitExercisePlans plan : list) {
            if (plan.getUserId() != null) {
                EasyfitUsers user = easyfitUsersService.selectEasyfitUsersById(plan.getUserId());
                plan.setUser(user);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出运动计划列表
     */
    @ApiOperation("导出运动计划列表")
    @Log(title = "运动计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EasyfitExercisePlans easyfitExercisePlans) {
        List<EasyfitExercisePlans> list = easyfitExercisePlansService.selectEasyfitExercisePlansList(easyfitExercisePlans);
        ExcelUtil<EasyfitExercisePlans> util = new ExcelUtil<EasyfitExercisePlans>(EasyfitExercisePlans.class);
        util.exportExcel(response, list, "运动计划数据");
    }

    /**
     * 获取运动计划详细信息
     */
    @ApiOperation("获取运动计划详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        EasyfitExercisePlans plan = easyfitExercisePlansService.selectEasyfitExercisePlansById(id);
        if (plan != null && plan.getUserId() != null) {
            EasyfitUsers user = easyfitUsersService.selectEasyfitUsersById(plan.getUserId());
            plan.setUser(user);
        }
        return success(plan);
    }

    /**
     * 新增运动计划
     */
    @ApiOperation("新增运动计划")
    @Log(title = "运动计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EasyfitExercisePlans easyfitExercisePlans) {
        return toAjax(easyfitExercisePlansService.insertEasyfitExercisePlans(easyfitExercisePlans));
    }

    /**
     * 修改运动计划
     */
    @ApiOperation("修改运动计划")
    @Log(title = "运动计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EasyfitExercisePlans easyfitExercisePlans) {
        return toAjax(easyfitExercisePlansService.updateEasyfitExercisePlans(easyfitExercisePlans));
    }

    /**
     * 删除运动计划
     */
    @ApiOperation("删除运动计划")
    @Log(title = "运动计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{planIds}")
    public AjaxResult remove(@ApiParam(value = "运动计划ID数组", required = true) @PathVariable Long[] planIds) {
        return toAjax(easyfitExercisePlansService.deleteEasyfitExercisePlansByPlanIds(planIds));
    }
}
